package cn.wolfcode.controller;


import cn.wolfcode.domain.Journal;
import cn.wolfcode.service.IJournalService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

@Controller
@RequestMapping("/journal")
public class JournalController {

    @Autowired
    private IJournalService journalService;


    @RequiredPermission(name="日志页面",expression="journal:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Journal> pageInfo = journalService.query(qo);
        model.addAttribute("PageInfo", pageInfo);
        return "journal/list";
    }


    @RequiredPermission(name="日志删除",expression="journal:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           journalService.delete(id);
        }
        return "redirect:/journal/list";
    }


    @RequiredPermission(name="日志新增/编辑",expression="journal:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Journal journal) {
        if (journal.getId() == null) {
            journalService.save(journal);
        } else {
            journalService.update(journal);
        }
        return "redirect:/journal/list";
    }


    @RequiredPermission(name="日志新增/编辑",expression="journal:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("journal",journalService.get(id));
        }
        return "journal/input";
    }
}
